package com.example.translateappjava.Activity.HomeFragmentActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.translateappjava.Activity.MainActivity;
import com.example.translateappjava.R;

public class GoodResultFragment extends Fragment {

    private String imageUrl;
    private String scientificBasis;
    private String recognitionResult;

    // 使用 newInstance 方法创建 Fragment 实例，并传递参数
    public static GoodResultFragment newInstance(String imageUrl, String scientificBasis, String recognitionResult) {
        GoodResultFragment fragment = new GoodResultFragment();
        Bundle args = new Bundle();
        args.putString("imageUrl", imageUrl);
        args.putString("scientificBasis", scientificBasis);
        args.putString("recognitionResult", recognitionResult);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            imageUrl = getArguments().getString("imageUrl");
            scientificBasis = getArguments().getString("scientificBasis");
            recognitionResult = getArguments().getString("recognitionResult");
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_good_result, container, false);

        TextView textViewScientificBasis = view.findViewById(R.id.tv_mid2);
        textViewScientificBasis.setText("   " + scientificBasis + "\n\n\n\n\n");

        TextView fruitname = view.findViewById(R.id.tv_mid1);
        fruitname.setText(recognitionResult);

        // 使用 Glide 加载 ImageURL
        ImageView imageView = view.findViewById(R.id.iv_mid1);
        if (imageUrl != null) {
            Glide.with(this)
                    .load(imageUrl)
                    .into(imageView);
        }

        view.findViewById(R.id.btn_exit2).setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            requireActivity().finish();
        });

        return view;
    }
}
